package com.no1ikun.view;

import com.no1ikun.bean.Teacher;
import com.no1ikun.publicdb.DB;
import com.no1ikun.service.TeacherService;
import com.no1ikun.service.TeacherServiceImpl;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

/**
 * 定义一个类,继承了JFrame类,这是一个窗体类
 */
public class LoginView extends JFrame {
    private String title;
    private int width;
    private int height;
    private JTextField number;
    private JPasswordField password;
    private JButton loginBut;
    private JButton registerBut;


    public LoginView(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;
        init();

    }

    public void init() {

        this.setTitle(title);
        double pmwidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth();//获得屏幕的宽
        double pmheight = Toolkit.getDefaultToolkit().getScreenSize().getHeight();//获取屏幕的高
        //2.设置宽高以及显示的位置,将窗体居中显示this.getWidth():获取窗体的宽,this.getHeight()获取屏幕的高
        this.setBounds((int) (pmwidth - width) / 2, (int) (pmheight - height) / 2, width, height);
        //给窗口一个布局,绝对布局
        setLayout(null);

//        this.getContentPane().setBackground(new Color(92, 205, 255));
        URL resource = LoginView.class.getResource("../img/bg.png");
        ImageIcon imageIcon = new ImageIcon(resource);
        JLabel bglable = new JLabel();
        bglable.setBounds(0, 0, 600, 400);
        bglable.setIcon(imageIcon);


        MyJLabel centerText = new MyJLabel("欢迎使用学籍管理系统", 300, 60, (int) (width - 300) / 2, 35, "微软雅黑", Font.BOLD, 30, Color.WHITE);
        MyJLabel numberText = new MyJLabel("账 号 ：", 86, 30, (int) (width - 280) / 2, 125, "微软雅黑", Font.BOLD, 20, Color.WHITE);
        MyJLabel passwordText = new MyJLabel("密 码 ：", 86, 30, (int) (width - 280) / 2, 190, "微软雅黑", Font.BOLD, 20, Color.WHITE);

        number = new JTextField();
        //设置显示的位置及宽高
        number.setBounds(250, 128, 200, 25);
        //去掉边框
        number.setBorder(null);
        number.setFont(new Font("微软雅黑",0,17));
        password = new JPasswordField();
        password.setBounds(250, 193, 200, 25);
        password.setFont(new Font("微软雅黑",0,17));
        password.setBorder(null);
        number.setText("T0001");
        password.setText("T000001");

        loginBut = new MyButton("登 录", 200, 280, 100, 40,Color.WHITE,Color.BLACK);
        registerBut = new MyButton("注 册", 360, 280, 100, 40,Color.WHITE,Color.BLACK);


        add(centerText);
        add(numberText);
        add(passwordText);
        add(number);
        add(password);
        add(loginBut);
        add(registerBut);
        add(bglable);






//        this.add(centerText);
//        this.add(numberText);
//        this.add(passwordText);
//        this.add(number);
//        this.add(password);
//        this.add(loginBut);
//        this.add(registerBut);

        URL resource2 = LoginView.class.getResource("../img/icon1.png");
        ImageIcon iconimg = new ImageIcon(resource2);
        Image image = iconimg.getImage();
        this.setIconImage(image);

        //进制窗口放大缩小
        this.setResizable(false);

        //监听关闭按钮,关闭后直接结束项目
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);


        //监听关闭窗口事件
     this.addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
             int option = JOptionPane.showConfirmDialog(null, "确定要退出系统吗?", "温馨提示", JOptionPane.YES_NO_OPTION);
             //判断用户选择的是哪个按钮
             if (option == JOptionPane.YES_OPTION) {
                 //退出整个系统
                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
             } else {
                 //不做任何处理
                 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
             }
         }
     });

        //监听注册按钮事件
        registerBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJDialog("老师注册信息", 530, 600);
                //隐藏登录界面
                setVisible(false);
            }
        });

        //监听登录按钮
        loginBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户输入的用户名和密码
                String numberText = number.getText();
                String passwordText = password.getText();
                System.out.println("用户名:" + numberText + ",密码:" + passwordText);

                //判断用户名和密码不能为空
                if (numberText.equals("") || passwordText.equals("")) {
                    JOptionPane.showMessageDialog(null, "用户名和密码不能为空!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                    return;//结束
                }
                System.out.println("用户名:" + numberText + ",密码:" + passwordText);
                //去完成用户登录的功能 service层请输入要搜索的内容
                TeacherService ss = new TeacherServiceImpl();
                Teacher teacher = ss.login_adm(numberText, passwordText);
                System.out.println(teacher == null ? "登录失败" : "登录成功");

                //如果登录成功
                if (teacher != null) {
                    //跳转到Main界面
                     DB.mainView = new MainView("220-欢迎【" + teacher.getTname() + "】", 1000, 600);
                     DB.mainView.clearPage();
                     DB.mainView.showPage("index");
                    //将登录成功的用户名保存到公共数据里面
                    DB.teacher = teacher;
                    //关闭登录界面
                    setVisible(false);
                } else {
                    //给出友好提示?
                    JOptionPane.showMessageDialog(null, "对不起,用户名或者密码错误!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                }


            }
        });

    }
}
